package org.qydjk.console.controller.seller;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.seller.HospitalDepartment;
import org.qydjk.common.pojo.seller.HospitalKeyDepartment;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.console.service.IHospitalDeptService;
import org.qydjk.console.service.impl.HospitalDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 机构科室管理
 */
@Controller
@RequestMapping("/sellerDept")
public class SellerDeptController extends BaseController{
    @Autowired
    private IHospitalDeptService hospitalDepartment;

    @RequestMapping("/gotoPage.do")
    public String gotoPage(){
        return "seller/sellerDeparment";
    }

    /**
     * 基础科室数据
     * @return
     */
    @RequestMapping("/baseDeptdg.do")
    @ResponseBody
    public Object deparmentDg(){
        List<Map<String,Object>> deist = new ArrayList<Map<String,Object>>();
        try {
            deist = baseService.findMapBySQL("select * from qy_hospital_department order by ord");
            return deist;
        } catch (Exception e) {
            e.printStackTrace();
            return deist;
        }
    }

    /**
     * 机构科室数据
     * @return
     */
    @RequestMapping("/sellerDeptdg.do")
    @ResponseBody
    public Object sellerDeptdg(Integer page, Integer rows, String sellerName,Integer sellerId){
        List<Map<String,Object>> deptlist = new ArrayList<Map<String,Object>>();
        HibernatePage pager = new HibernatePage(page, rows);
        try {
            String sql = "SELECT h.id,h.hospital_department_id as hospitalDepartmentId,h.seller_id as sellerId,h.intro,h.achievement,h.ord,h.update_time as updateTime,s.name as sellerName,d.name\n" +
                    "FROM qy_hospital_key_department h,qy_seller s,qy_hospital_department d \n" +
                    "where s.id = h.seller_id and h.hospital_department_id=d.id\n" ;
            String countsql = "SELECT count(h.id)\n" +
                    "FROM qy_hospital_key_department h,qy_seller s,qy_hospital_department d \n" +
                    "where s.id = h.seller_id and h.hospital_department_id=d.id\n" ;
            if(sellerId!=null){
                sql+=" and h.seller_id= "+sellerId;
                countsql+=" and h.seller_id= "+sellerId;
            }
            if(!StringUtil.isEmptyOrNull(sellerName)){
                sql+=" and s.name like '%"+sellerName+"%'";
                countsql+=" and s.name like '%"+sellerName+"%'";
            }
            int count = baseService.getRecordCountSQL(countsql);
            deptlist = baseService.findMapBySQL(sql,pager);
            return new EasyUIResponse(count,deptlist);
        } catch (Exception e) {
            e.printStackTrace();
            return deptlist;
        }
    }
    @RequestMapping("/addOredit.do")
    @ResponseBody
    public Object addOredit(HospitalDepartment hospitalDepartment){
        try {
            baseService.saveOrUpdate(hospitalDepartment);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.toString());
        }
    }
    //删除基础科室
    @RequestMapping("/del.do")
    @ResponseBody
    public Object del(Integer id){
        try {
            hospitalDepartment.delHospialDept(id);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.toString());
        }
    }
    //删除机构科室
    @RequestMapping("/delShopDept.do")
    @ResponseBody
    public Object delShopDept(Integer id){
        try {
            HospitalKeyDepartment department = baseService.get(HospitalKeyDepartment.class,id);
            hospitalDepartment.delete(department);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.toString());
        }
    }
    @RequestMapping("/addShopdept.do")
    @ResponseBody
    public Object addShopdept(HospitalKeyDepartment keyDepartment){
        try {
            keyDepartment.setUpdateTime(new Date());
            if(keyDepartment.getId()==null){
                keyDepartment.setCreateTime(new Date());
                hospitalDepartment.saveOrUpdate(keyDepartment);
            }else{
                HospitalKeyDepartment old = hospitalDepartment.get(HospitalKeyDepartment.class,keyDepartment.getId());
                BeanUtil.copyProperties(keyDepartment,old);
                hospitalDepartment.saveOrUpdate(old);
            }
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.toString());
        }
    }

    /**
     * 机构列表数据
     * @return
     */
    @RequestMapping("/sellerdg.do")
    @ResponseBody
    public Object sellerdg(Integer page, Integer rows, String sellerName){
        List<Map<String,Object>> deptlist = new ArrayList<Map<String,Object>>();
        HibernatePage pager = new HibernatePage(page, rows);
        try {
            String sql = "select name,id,two_area_name from qy_seller where 1=1";
            String countsql = "select count(*) from qy_seller where 1=1";
            if(!StringUtil.isEmptyOrNull(sellerName)){
                sql+=" and name like '%"+sellerName+"%'";
                countsql+=" and name like '%"+sellerName+"%'";
            }
            int count = baseService.getRecordCountSQL(countsql);
            deptlist = baseService.findMapBySQL(sql,pager);
            return new EasyUIResponse(count,deptlist);
        } catch (Exception e) {
            e.printStackTrace();
            return deptlist;
        }
    }
    /**
     * 基础科室数据
     * @return
     */
    @RequestMapping("/baseDeptcb.do")
    @ResponseBody
    public Object baseDeptcb(){
        List<Map<String,Object>> deptlist = new ArrayList<Map<String,Object>>();
        try {
            String sql = "select id,name from qy_hospital_department";
            deptlist = baseService.findMapBySQL(sql);
            return deptlist;
        } catch (Exception e) {
            e.printStackTrace();
            return deptlist;
        }
    }
}
